<template>

  <el-dialog :visible="permDialog" title="分配权限" @close="closeFn">
    <el-tree ref="tree" node-key="id" :data="permissions" :props="{label: 'title'}" show-checkbox check-strictly />
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="submit">确认</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { byIdRelosInfo, getPermissions, updateRoleInfo } from '@/api/roles'
export default {
  props: {
    permDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      permissions: [],
      permId: [],
      curRoId: ''
    }
  },
  created() {
    this.getPermissionsInfo()
  },
  methods: {
    async submit() {
      this.permId = this.$refs.tree.getCheckedKeys()
      await updateRoleInfo({ id: this.curRoId, permissions: this.permId })
      this.closeFn()
      this.$message({
        message: '分配成功',
        type: 'success'
      })
    },
    closeFn() {
      this.$emit('update:permDialog', false)
      this.permId = []
    },
    async  getPermissionsInfo() {
      const res = await getPermissions()
      //   console.log(res)
      this.permissions = res.data
    },
    async getPermissions(id) {
      this.curRoId = id
      const res = await byIdRelosInfo(id)
      console.log(res)
      this.permId = res.data.permissions.map(item => item.id)
      this.$refs.tree.setCheckedKeys(this.permId)
    }
  }
}
</script>

<style>

</style>
